home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / ccastles.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  10KB  |  384 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8. #include "driver.h"
  9. #include "vidhrdw/generic.h"
  10.  
  11.  
  12. static struct osd_bitmap *sprite_bm;
  13. static struct osd_bitmap *maskbitmap;
  14.  
  15. static int flipscreen;
  16. static int screen_flipped;
  17.  
  18. unsigned char *ccastles_screen_addr;
  19. unsigned char *ccastles_screen_inc;
  20. unsigned char *ccastles_screen_inc_enable;
  21. unsigned char *ccastles_sprite_bank;
  22. unsigned char *ccastles_scrollx;
  23. unsigned char *ccastles_scrolly;
  24.  
  25. /***************************************************************************
  26.  
  27.   Convert the color PROMs into a more useable format.
  28.  
  29.   Crystal Castles doesn't have a color PROM. It uses RAM to dynamically
  30.   create the palette. The resolution is 9 bit (3 bits per gun). The palette
  31.   contains 32 entries, but it is accessed through a memory windows 64 bytes
  32.   long: writing to the first 32 bytes sets the msb of the red component to 0,
  33.   while writing to the last 32 bytes sets it to 1.
  34.   The first 16 entries are used for sprites; the last 16 for the background
  35.   bitmap.
  36.  
  37.   I don't know the exact values of the resistors between the RAM and the
  38.   RGB output, I assumed the usual ones.
  39.   bit 8 -- inverter -- 220 ohm resistor  -- RED
  40.   bit 7 -- inverter -- 470 ohm resistor  -- RED
  41.         -- inverter -- 1  kohm resistor  -- RED
  42.         -- inverter -- 220 ohm resistor  -- BLUE
  43.         -- inverter -- 470 ohm resistor  -- BLUE
  44.         -- inverter -- 1  kohm resistor  -- BLUE
  45.         -- inverter -- 220 ohm resistor  -- GREEN
  46.         -- inverter -- 470 ohm resistor  -- GREEN
  47.   bit 0 -- inverter -- 1  kohm resistor  -- GREEN
  48.  
  49. ***************************************************************************/
  50. WRITE_HANDLER( ccastles_paletteram_w )
  51. {
  52.     int r,g,b;
  53.     int bit0,bit1,bit2;
  54.  
  55.  
  56.     r = (data & 0xC0) >> 6;
  57.     b = (data & 0x38) >> 3;
  58.     g = (data & 0x07);
  59.     /* a write to offset 32-63 means to set the msb of the red component */
  60.     if (offset & 0x20) r += 4;
  61.  
  62.     /* bits are inverted */
  63.     r = 7-r;
  64.     g = 7-g;
  65.     b = 7-b;
  66.  
  67.     bit0 = (r >> 0) & 0x01;
  68.     bit1 = (r >> 1) & 0x01;
  69.     bit2 = (r >> 2) & 0x01;
  70.     r = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  71.     bit0 = (g >> 0) & 0x01;
  72.     bit1 = (g >> 1) & 0x01;
  73.     bit2 = (g >> 2) & 0x01;
  74.     g = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  75.     bit0 = (b >> 0) & 0x01;
  76.     bit1 = (b >> 1) & 0x01;
  77.     bit2 = (b >> 2) & 0x01;
  78.     b = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  79.  
  80.     palette_change_color(offset & 0x1f,r,g,b);
  81. }
  82.  
  83.  
  84.  
  85. /***************************************************************************
  86.  
  87.   Start the video hardware emulation.
  88.  
  89. ***************************************************************************/
  90. int ccastles_vh_start(void)
  91. {
  92.     if ((tmpbitmap = osd_create_bitmap(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  93.         return 1;
  94.  
  95.     if ((maskbitmap = osd_create_bitmap(Machine->drv->screen_width,Machine->drv->screen_height)) == 0)
  96.     {
  97.         osd_free_bitmap(tmpbitmap);
  98.         return 1;
  99.     }
  100.  
  101.     if ((sprite_bm = osd_create_bitmap(16,16)) == 0)
  102.     {
  103.         osd_free_bitmap(maskbitmap);
  104.         osd_free_bitmap(tmpbitmap);
  105.         return 1;
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
  111.  
  112.  
  113. /***************************************************************************
  114.  
  115.   Stop the video hardware emulation.
  116.  
  117. ***************************************************************************/
  118. void ccastles_vh_stop(void)
  119. {
  120.     osd_free_bitmap(sprite_bm);
  121.     osd_free_bitmap(maskbitmap);
  122.     osd_free_bitmap(tmpbitmap);
  123. }
  124.  
  125.  
  126.  
  127. READ_HANDLER( ccastles_bitmode_r )
  128. {
  129.     int addr;
  130.  
  131.     addr = (ccastles_screen_addr[1]<<7) | (ccastles_screen_addr[0]>>1);
  132.  
  133.     /* is the address in videoram? */
  134.     if ((addr >= 0x0c00) && (addr < 0x8000))
  135.     {
  136.         /* auto increment in the x-direction if it's enabled */
  137.         if (!ccastles_screen_inc_enable[0])
  138.         {
  139.             if (!ccastles_screen_inc[0])
  140.                 ccastles_screen_addr[0] ++;
  141.             else
  142.                 ccastles_screen_addr[0] --;
  143.         }
  144.  
  145.         /* auto increment in the y-direction if it's enabled */
  146.         if (!ccastles_screen_inc_enable[1])
  147.         {
  148.             if (!ccastles_screen_inc[1])
  149.                 ccastles_screen_addr[1] ++;
  150.             else
  151.                 ccastles_screen_addr[1] --;
  152.         }
  153.  
  154.         addr -= 0xc00;
  155.         if (ccastles_screen_addr[0] & 0x01)
  156.             return ((videoram[addr] & 0x0f) << 4);
  157.         else
  158.             return (videoram[addr] & 0xf0);
  159.     }
  160.  
  161.     return 0;
  162. }
  163.  
  164. WRITE_HANDLER( ccastles_bitmode_w )
  165. {
  166.     int addr;
  167.  
  168.  
  169.     addr = (ccastles_screen_addr[1] << 7) | (ccastles_screen_addr[0] >> 1);
  170.  
  171.     /* is the address in videoram? */
  172.     if ((addr >= 0x0c00) && (addr < 0x8000))
  173.     {
  174.         int x,y,j;
  175.         int mode;
  176.  
  177.         addr -= 0xc00;
  178.  
  179.         if (ccastles_screen_addr[0] & 0x01)
  180.         {
  181.             mode = (data >> 4) & 0x0f;
  182.             videoram[addr] = (videoram[addr] & 0xf0) | mode;
  183.         }
  184.         else
  185.         {
  186.             mode = (data & 0xf0);
  187.             videoram[addr] = (videoram[addr] & 0x0f) | mode;
  188.         }
  189.  
  190.         j = 2*addr;
  191.         x = j%256;
  192.         y = j/256;
  193.         if (!flipscreen)
  194.         {
  195.             plot_pixel(tmpbitmap, x  , y, Machine->pens[16 + ((videoram[addr] & 0xf0) >> 4)]);
  196.             plot_pixel(tmpbitmap, x+1, y, Machine->pens[16 +  (videoram[addr] & 0x0f)      ]);
  197.  
  198.             /* if bit 3 of the pixel is set, background has priority over sprites when */
  199.             /* the sprite has the priority bit set. We use a second bitmap to remember */
  200.             /* which pixels have priority. */
  201.             plot_pixel(maskbitmap, x  , y, videoram[addr] & 0x80);
  202.             plot_pixel(maskbitmap, x+1, y, videoram[addr] & 0x08);
  203.         }
  204.         else
  205.         {
  206.             y = 231-y;
  207.             x = 254-x;
  208.             if (y >= 0)
  209.             {
  210.                 plot_pixel(tmpbitmap, x+1, y, Machine->pens[16 + ((videoram[addr] & 0xf0) >> 4)]);
  211.                 plot_pixel(tmpbitmap, x  , y, Machine->pens[16 +  (videoram[addr] & 0x0f)      ]);
  212.  
  213.                 /* if bit 3 of the pixel is set, background has priority over sprites when */
  214.                 /* the sprite has the priority bit set. We use a second bitmap to remember */
  215.                 /* which pixels have priority. */
  216.                 plot_pixel(maskbitmap, x+1, y, videoram[addr] & 0x80);
  217.                 plot_pixel(maskbitmap, x  , y, videoram[addr] & 0x08);
  218.             }
  219.         }
  220.     }
  221.  
  222.     /* auto increment in the x-direction if it's enabled */
  223.     if (!ccastles_screen_inc_enable[0])
  224.     {
  225.         if (!ccastles_screen_inc[0])
  226.             ccastles_screen_addr[0] ++;
  227.         else
  228.             ccastles_screen_addr[0] --;
  229.     }
  230.  
  231.     /* auto increment in the y-direction if it's enabled */
  232.     if (!ccastles_screen_inc_enable[1])
  233.     {
  234.         if (!ccastles_screen_inc[1])
  235.             ccastles_screen_addr[1] ++;
  236.         else
  237.             ccastles_screen_addr[1] --;
  238.     }
  239.  
  240. }
  241.  
  242. WRITE_HANDLER( ccastles_flipscreen_w )
  243. {
  244.     if (flipscreen != (data & 1))
  245.     {
  246.         flipscreen = data & 1;
  247.  
  248.         screen_flipped = 1;
  249.     }
  250. }
  251.  
  252.  
  253.  
  254. /***************************************************************************
  255.  
  256.   Draw the game screen in the given osd_bitmap.
  257.   Do NOT call osd_update_display() from this function, it will be called by
  258.   the main emulation engine.
  259.  
  260. ***************************************************************************/
  261. static void redraw_bitmap(void)
  262. {
  263.     int x, y;
  264.     int screen_addr0_save, screen_addr1_save, screen_inc_enable0_save, screen_inc_enable1_save;
  265.  
  266.  
  267.     /* save out registers */
  268.     screen_addr0_save = ccastles_screen_addr[0];
  269.     screen_addr1_save = ccastles_screen_addr[1];
  270.  
  271.     screen_inc_enable0_save = ccastles_screen_inc_enable[0];
  272.     screen_inc_enable1_save = ccastles_screen_inc_enable[1];
  273.  
  274.     ccastles_screen_inc_enable[0] = ccastles_screen_inc_enable[1] = 1;
  275.  
  276.  
  277.     /* redraw bitmap */
  278.     for (y = 0; y < 256; y++)
  279.     {
  280.         ccastles_screen_addr[1] = y;
  281.  
  282.         for (x = 0; x < 256; x++)
  283.         {
  284.             ccastles_screen_addr[0] = x;
  285.  
  286.             ccastles_bitmode_w(0, ccastles_bitmode_r(0));
  287.         }
  288.     }
  289.  
  290.  
  291.     /* restore registers */
  292.     ccastles_screen_addr[0] = screen_addr0_save;
  293.     ccastles_screen_addr[1] = screen_addr1_save;
  294.  
  295.     ccastles_screen_inc_enable[0] = screen_inc_enable0_save;
  296.     ccastles_screen_inc_enable[1] = screen_inc_enable1_save;
  297. }
  298.  
  299.  
  300. void ccastles_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  301. {
  302.     int offs;
  303.     unsigned char *spriteaddr;
  304.     int scrollx,scrolly;
  305.  
  306.  
  307.     if (palette_recalc() || screen_flipped)
  308.     {
  309.         redraw_bitmap();
  310.         screen_flipped = 0;
  311.     }
  312.  
  313.  
  314.     scrollx = 255 - *ccastles_scrollx;
  315.     scrolly = 255 - *ccastles_scrolly;
  316.  
  317.     if (flipscreen)
  318.     {
  319.         scrollx = 254 - scrollx;
  320.         scrolly = 231 - scrolly;
  321.     }
  322.  
  323.     copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,
  324.                      &Machine->drv->visible_area,
  325.                         TRANSPARENCY_NONE,0);
  326.  
  327.  
  328.     if (*ccastles_sprite_bank)
  329.         spriteaddr = spriteram;
  330.     else
  331.         spriteaddr = spriteram_2;
  332.  
  333.  
  334.     /* Draw the sprites */
  335.     for (offs = 0; offs < spriteram_size; offs += 4)
  336.     {
  337.         int i,j;
  338.         int x,y;
  339.  
  340.         /* Get the X and Y coordinates from the MOB RAM */
  341.         x = spriteaddr[offs+3];
  342.         y = 216 - spriteaddr[offs+1];
  343.  
  344.         if (spriteaddr[offs+2] & 0x80)    /* background can have priority over the sprite */
  345.         {
  346.             fillbitmap(sprite_bm,Machine->gfx[0]->colortable[7],0);
  347.             drawgfx(sprite_bm,Machine->gfx[0],
  348.                     spriteaddr[offs],1,
  349.                     flipscreen,flipscreen,
  350.                     0,0,
  351.                     0,TRANSPARENCY_PEN,7);
  352.  
  353.             for (j = 0;j < 16;j++)
  354.             {
  355.                 if (y + j >= 0)    /* avoid accesses out of the bitmap boundaries */
  356.                 {
  357.                     for (i = 0;i < 8;i++)
  358.                     {
  359.                         int pixa,pixb;
  360.  
  361.                         pixa = read_pixel(sprite_bm, i, j);
  362.                         pixb = read_pixel(maskbitmap, (x+scrollx+i)%256, (y+scrolly+j)%232);
  363.  
  364.                         /* if background has priority over sprite, make the */
  365.                         /* temporary bitmap transparent */
  366.                         if (pixb != 0 && (pixa != Machine->gfx[0]->colortable[0]))
  367.                             plot_pixel(sprite_bm, i, j, Machine->gfx[0]->colortable[7]);
  368.                     }
  369.                 }
  370.             }
  371.  
  372.             copybitmap(bitmap,sprite_bm,0,0,x,y,&Machine->drv->visible_area,TRANSPARENCY_PEN,Machine->gfx[0]->colortable[7]);
  373.         }
  374.         else
  375.         {
  376.             drawgfx(bitmap,Machine->gfx[0],
  377.                     spriteaddr[offs],1,
  378.                     flipscreen,flipscreen,
  379.                     x,y,
  380.                     &Machine->drv->visible_area,TRANSPARENCY_PEN,7);
  381.         }
  382.     }
  383. }
  384.